import heapq
from collections import defaultdict

n, m = map(int, input().split())
edges1 = defaultdict(list)
edges_all = defaultdict(list)
for _ in range(m):
    a, b, c, d = map(int, input().split())
    edges_all[a].append((b, c))
    edges_all[b].append((a, c))
    if not d:
        edges1[a].append((b, c))
        edges1[b].append((a, c))
heap = [(0, 1)]
distances = [float('inf')] * (n + 1)
distances[1] = 0
while heap:
    curr_dis, curr = heapq.heappop(heap)
    if distances[curr] < curr_dis:
        continue
    if curr == n:
        distance1 = curr_dis
        break
    for next_node, next_distance in edges1[curr]:
        distances[next_node] = next_distance + curr_dis
        heapq.heappush(heap, (next_distance + curr_dis, next_node))

heap = [(0, 1)]
distances = [float('inf')] * (n + 1)
distances[1] = 0
while heap:
    curr_dis, curr = heapq.heappop(heap)
    if distances[curr] < curr_dis:
        continue
    if curr == n:
        distance2 = curr_dis
        break
    for next_node, next_distance in edges_all[curr]:
        distances[next_node] = next_distance + curr_dis
        heapq.heappush(heap, (next_distance + curr_dis, next_node))

print(distance1 - distance2)
